﻿using BITUI.Element;
using System;
using System.Collections.Generic;
using System.Text;

namespace BITUI.Tile
{
    public interface IFillDraw
    {
        void FillDraw(IWindowRender render, int x, int y, int w, int h, byte colorReplace = 0);
    }
    public class Scale9 : IFillDraw
    {
        public RenderTile topleft;
        public RenderTile top;
        public RenderTile topright;
        public RenderTile left;
        public RenderTile center;
        public RenderTile right;
        public RenderTile bottomleft;
        public RenderTile bottom;
        public RenderTile bottomright;
        public static Scale9 CreateSingleBorderScale9(Bank bank, string name)
        {
            Scale9 s9 = new Scale9();
            s9.topleft = bank.GetTileByName(name + "_lt");
            s9.top = bank.GetTileByName(name + "_t");
            s9.topright = bank.GetTileByName(name + "_rt");
            s9.left = bank.GetTileByName(name + "_l");
            s9.center = null;
            s9.right = bank.GetTileByName(name + "_r");
            s9.bottomleft = bank.GetTileByName(name + "lb");
            s9.bottom = bank.GetTileByName(name + "b");
            s9.bottomright = bank.GetTileByName(name + "rb");

            if (s9.topleft == null)
                s9.topleft = bank.CreateTile(name + "_lt", BITUI.Tile.TileTool.GenOnePixelBorder(true, true, false, false));
            if (s9.top == null)
                s9.top = bank.CreateTile(name + "_t", BITUI.Tile.TileTool.GenOnePixelBorder(false, true, false, false));
            if (s9.topright == null)
                s9.topright = bank.CreateTile(name + "_rt", BITUI.Tile.TileTool.GenOnePixelBorder(false, true, true, false));

            if (s9.left == null)
                s9.left = bank.CreateTile(name + "_l", BITUI.Tile.TileTool.GenOnePixelBorder(true, false, false, false));

            if (s9.right == null)
                s9.right = bank.CreateTile(name + "_r", BITUI.Tile.TileTool.GenOnePixelBorder(false, false, true, false));

            if (s9.bottomleft == null)
                s9.bottomleft = bank.CreateTile(name + "_lb", BITUI.Tile.TileTool.GenOnePixelBorder(true, false, false, true));
            if (s9.bottom == null)
                s9.bottom = bank.CreateTile(name + "_b", BITUI.Tile.TileTool.GenOnePixelBorder(false, false, false, true));
            if (s9.bottomright == null)
                s9.bottomright = bank.CreateTile(name + "_rb", BITUI.Tile.TileTool.GenOnePixelBorder(false, false, true, true));

            return s9;
        }
        public static Scale9 CreateTwoBorderScale9(Bank bank, string name)
        {
            Scale9 s9 = new Scale9();
            s9.topleft = bank.GetTileByName(name + "_lt");
            s9.top = bank.GetTileByName(name + "_t");
            s9.topright = bank.GetTileByName(name + "_rt");
            s9.left = bank.GetTileByName(name + "_l");
            s9.center = null;
            s9.right = bank.GetTileByName(name + "_r");
            s9.bottomleft = bank.GetTileByName(name + "lb");
            s9.bottom = bank.GetTileByName(name + "b");
            s9.bottomright = bank.GetTileByName(name + "rb");

            if (s9.topleft == null)
                s9.topleft = bank.CreateTile(name + "_lt", BITUI.Tile.TileTool.GenTwoPixelBorder(true, true, false, false));
            if (s9.top == null)
                s9.top = bank.CreateTile(name + "_t", BITUI.Tile.TileTool.GenTwoPixelBorder(false, true, false, false));
            if (s9.topright == null)
                s9.topright = bank.CreateTile(name + "_rt", BITUI.Tile.TileTool.GenTwoPixelBorder(false, true, true, false));

            if (s9.left == null)
                s9.left = bank.CreateTile(name + "_l", BITUI.Tile.TileTool.GenTwoPixelBorder(true, false, false, false));

            if (s9.right == null)
                s9.right = bank.CreateTile(name + "_r", BITUI.Tile.TileTool.GenTwoPixelBorder(false, false, true, false));

            if (s9.bottomleft == null)
                s9.bottomleft = bank.CreateTile(name + "_lb", BITUI.Tile.TileTool.GenTwoPixelBorder(true, false, false, true));
            if (s9.bottom == null)
                s9.bottom = bank.CreateTile(name + "_b", BITUI.Tile.TileTool.GenTwoPixelBorder(false, false, false, true));
            if (s9.bottomright == null)
                s9.bottomright = bank.CreateTile(name + "_rb", BITUI.Tile.TileTool.GenTwoPixelBorder(false, false, true, true));

            return s9;
        }
        public void FillDraw(IWindowRender render, int x, int y, int w, int h, byte colorReplace)
        {
            //太小了没法画


            int midwidth = w - this.topleft.srcrect.w - this.bottomright.srcrect.w;
            int midheight = h - this.topleft.srcrect.h - this.bottomright.srcrect.h;
            if (midwidth < 0 || midheight < 0)
                return;
            int x1 = x;
            int x2 = x1 + this.topleft.srcrect.w;
            int x3 = x2 + midwidth;
            int y1 = y;
            int y2 = y1 + this.topleft.srcrect.h;
            int y3 = y2 + midheight;
            render.DrawTile(this.topleft, x1, y1, colorReplace);
            render.DrawTile(this.top, x2, y1, colorReplace, midwidth / this.top.srcrect.w);//h scale
            render.DrawTile(this.topright, x3, y1, colorReplace);

            render.DrawTile(this.left, x1, y2, colorReplace, 1, midheight / this.left.srcrect.h);//v scale
            if (this.center != null)
            {
                render.DrawTile(this.center, x2, y2, colorReplace, midwidth / this.center.srcrect.w, midheight / this.center.srcrect.h);//hv scale
            }
            render.DrawTile(this.right, x3, y2, colorReplace, 1, midheight / this.right.srcrect.h);//v scale

            render.DrawTile(this.bottomleft, x1, y3, colorReplace);
            render.DrawTile(this.bottom, x2, y3, colorReplace, midwidth / this.bottom.srcrect.w, 1);//h scale
            render.DrawTile(this.bottomright, x3, y3, colorReplace);
        }
    }
}
